<?php

namespace App\Utils\Sms;

use GuzzleHttp\Exception\GuzzleException;
class ZhuTongSms implements SmsInterface
{
    public string $signName = '绒博汇';

    public function __construct(string $signName)
    {
        $this->signName = $signName;
    }


    /**
     * 签名处理
     * @param bool $includeBracket
     * @return string
     */
    public function setSignName(bool $includeBracket): string
    {
        return $includeBracket ? '【'. $this->signName . '】' : $this->signName;
    }

    /**
     * 发送验证码
     * @param string $mobile
     * @param int $verifyCode
     * @return bool
     * @throws GuzzleException
     */
    public function send(string $mobile, int $verifyCode): bool
    {
        $timestamp = time();
        $data = [
            'username'  => 'BLRJhy',
            'password'  => md5(md5('PPbhLsFX') . $timestamp),
            'tKey'      => $timestamp,
            'signature' => $this->setSignName(true),
            'tpId'      => 153362,
            'records'   => array_map(function ($mobile) use ($verifyCode) {
                return [
                    'mobile' => $mobile,
                    'tpContent' => ['verify' => $verifyCode]
                ];
            }, array_filter(explode(',', $mobile)))
        ];
        $response = GuzzlePostJson('https://api.mix2.zthysms.com', '/v2/sendSmsTp', $data, [
            'Content-Type' => 'application/json'
        ]);
        return (bool)$response;
    }
}
